package com.ruoyi.spin.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author Lorry
 * @description
 * @since 2022-01-17 20:50
 */
@Data
public class Task implements Delayed {
  /** 延迟时间 */
  @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
  private final long time;

  public String name;

  public Task(String name, long time, TimeUnit unit) {
    this.name = name;
    this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
  }

  @Override
  public long getDelay(TimeUnit unit) {
    return time - System.currentTimeMillis();
  }

  @Override
  public int compareTo(Delayed o) {
    Task task = (Task) o;
    long diff = this.time - task.time;
    if (diff <= 0) {
      return -1;
    } else {
      return 1;
    }
  }
}
